package com.tledu.production.controller.plan;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.plan.OrderManagement;
import com.tledu.production.model.plan.ProductManagement;
import com.tledu.production.service.plan.PlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/ProductController")
public class ProductController {
    private final PlanService planService;
    @GetMapping("/plan/ProductManagement")
    public String getAllO(Model model,Integer pageNum, Integer pageSize ){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<ProductManagement> product=planService.getAllP();
        PageInfo<ProductManagement> page = PageInfo.of(product);//把获取到的数据封装到page中
        model.addAttribute("getAllP",page.getList());
        model.addAttribute("page", page);
        model.addAttribute("pages",new int[page.getPages()]);
        return "plan/ProductManagement";
    }
    @GetMapping("/plan/product-add")
    public String addP(){
        return "plan/add/product-add";
    }
    @PostMapping("/plan/product-add")
    @ResponseBody
    public AjaxResult<Void> addP(ProductManagement productManagement){
        planService.insertP(productManagement);
        return new AjaxResult<>();
    }
    @GetMapping("/plan/ProductManagement/delete")
    @ResponseBody
        public JSONObject deleteP(String id){
            int delete= planService.deleteP(id);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("success",delete);
             return jsonObject;
        }
    //********************编辑**************************************
    @GetMapping("/edit/product-edit/updatep/{productId}")
    public String updateP(Model model, @PathVariable String productId){
        ProductManagement productManagement=planService.editP(productId);
        model.addAttribute("productManagement", productManagement);

        return "plan/edit/product-edit";
    }
    @PostMapping("/edit/product-edit/update/{productId}")
    @ResponseBody
  public AjaxResult<Void> updateP(@PathVariable String productId ,ProductManagement productManagement){
        productManagement.setProductId(productId);
        planService.updateP(productManagement);
        return new AjaxResult<>();
  }
    @DeleteMapping("/depBatchDelete")
    @ResponseBody
    public AjaxResult<ProductManagement> batchDelete(String idList){
        String[] array=idList.split(",");
        for (String s : array){
           planService.deleteP(s);
        }
        return new AjaxResult<>("删除成功");
    }
    //富文本
    @GetMapping("/richTextP/{productId}")
    public String richTextO(Model model,  @PathVariable String productId){
       ProductManagement productManagement=planService.editP(productId);
        model.addAttribute("productManagement",productManagement);
            return "plan/richText/productRichText";
    }
}
